package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Line;
import com.example.front.entity.Workshop;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LineMapper {
    @Insert("INSERT INTO `line`" +
            " (`tenant_id`,`name`, `workshop_id`, `user_id`) VALUES" +
            " (#{tenantId}, #{name}, #{workshopId}, #{userId})")
    void create(Line line);

    @Select("<script>" +
            "SELECT * FROM `line` " +
            "<where>" +
            " `tenant_id`=#{line.tenantId} " +
            "<if test='line.id!=null'> AND `id`=#{line.id}</if>" +
            "<if test='line.name!=null'> AND `name` LIKE CONCAT('%',#{line.name},'%') </if>" +
            "<if test='line.workshopId!=null'> AND `workshop_id`=#{line.workshopId} </if>" +
            "<if test='line.userId!=null'> AND `user_id`=#{line.userId} </if>" +
            "</where>" +
            "  AND `tenant_id`=#{line.tenantId} AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Line> findAll(@Param("line") Line line, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT * FROM `line` " +
            "<where>" +
            " `tenant_id`=#{line.tenantId} " +
            "<if test='line.id!=null'> AND `id`!=#{line.id}</if>" +
            "<if test='line.name!=null'> AND `name`=#{line.name} </if>" +
            "</where>" +
            " AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    Line findByName(@Param("line") Line line);

    @Select("SELECT * FROM `line` WHERE `tenant_id`=#{line.tenantId} AND `name` = #{line.name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    List<Line> findLineByName(@Param("line") Line line);

    @Select("SELECT * FROM `line` WHERE `tenant_id`=#{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Line findLineById(@Param("tenantId") Long tenantId,@Param("id") Long id);

    @Select("SELECT * FROM `line` WHERE `id` = #{id} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Line findLineByID(@Param("id") Long id);

    @Select("SELECT count(*) FROM `line` WHERE `tenant_id`=#{tenantId} AND `workshop_id` = #{workshopId} AND `status`!=" + User.STATUS_DELETE)
    long findByWorkshopId(@Param("workshopId") Long workshopId,@Param("tenantId") Long tenantId);

    @Update("UPDATE `line` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(Line line);

    @Update("UPDATE `line` SET `name` = #{name} , `workshop_id` = #{workshopId}, `user_id` = #{userId}" +
            " WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    int update(Line line);

    @Select("<script>" +
            "SELECT * FROM user WHERE `id` IN " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<User> findUserByIds(@Param("ids") List<Long> ids);

    @Select("<script>" +
            "SELECT * FROM workshop WHERE `id` IN " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    List<Workshop> findWorkshopByIds(@Param("ids") List<Long> ids);

    @Select("<script>" +
            "SELECT * FROM `line` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='lineIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    List<Line> findLineByIds(@Param("lineIds") List<Long> lineIds);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `line`" +
            "<where>" +
            "line.tenant_id = #{line.tenantId} " +
            "<if test='line.name!=null'> AND `name` LIKE CONCAT('%',#{line.name},'%') </if>" +
            "<if test='line.id!=null'> AND `id`=#{line.id}</if>" +
            "<if test='line.workshopId!=null'> AND `workshop_id`=#{line.workshopId}</if>" +
            "<if test='line.userId!=null'> AND `user_id`=#{line.userId}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE+
            " AND `tenant_id`=#{line.tenantId} " +
            "</script>")
    long count(@Param("line") Line line);
}
